<template>
	<div class="_wrapper _rel">
		<div id="playWnd" ref="playWnd" class="_page" />
		<button class="_btn-1 _m-t-10" @click="closeVideo">
			断开视频链接
		</button>
	</div>
</template>

<script>
	import HaikangConfig from "../HaikangConfig";
	import ToolWindow from "@/tool/ToolWindow";
	import ToolWaitObj from "@/tool/ToolWaitObj";
	import ToolLoadJs from "@/tool/ToolLoadJs.js";
	
	let dom;
	let oWebControl;
	
	export default {
		name: "VideoHk",
		mounted() {
			dom = this.$refs.playWnd;
			this.log('开始加载海康插件');
	
			ToolLoadJs.loadJSList([
				"http://115.29.192.252:39000/cdn/haikang/jquery-1.12.4.min.js",
				"http://115.29.192.252:39000/cdn/haikang/jsencrypt.min.js",
				"http://115.29.192.252:39000/cdn/haikang/jsWebControl-1.0.0.min.js"
			], () => {
				ToolWaitObj.waitObj('WebControl').then(WebControl => {
					this.log('开始初始化海康插件');
					initHaikang(WebControl, dom.offsetWidth, dom.offsetHeight)
				})
			})
		},
		beforeUnmount() {
			// closeVideo()
			this.log('页面销毁事件');
		},
		methods: {
			closeVideo() {
				closeVideo()
			}
		}
	}
	
	function closeVideo() {
		if (oWebControl != null) {
			oWebControl.JS_HideWnd(); // 先让窗口隐藏，规避插件窗口滞后于浏览器消失问题
			oWebControl.JS_Disconnect().then(function() {
					log('视频链接断开成功！')
				},
				function() {
					log('视频链接断开异常......')
				});
		}
	}
	
	function initHaikang(WebControl, width, height) {
		// 创建 WebControl 实例
		oWebControl = new WebControl({
			szPluginContainer: "playWnd", // 指定 DIV 窗口标识
			iServicePortStart: 15900, // 指定起止端口号，建议使用该值
			iServicePortEnd: 15900,
			// 用于 IE10 使用 ActiveX 的 clsid
			szClassId: "23BF3B0A-2C56-4D97-9C03-0CB103AA8F11",
			cbConnectSuccess: function() {
				log('初始化海康插件成功')
				// 实例创建成功后启动服务
				oWebControl.JS_StartService("window", {
					dllPath: "./VideoPluginConnect.dll" //
				}).then(function() { // 启动插件服务成功
					// 设置消息回调
					oWebControl.JS_SetWindowControlCallback({
						cbIntegrationCallBack: () => {}
					})
	
					//JS_CreateWnd 创建视频播放窗口，宽高可设定
					oWebControl.JS_CreateWnd("playWnd", width, height).then(async function() {
						HaikangConfig.generateHkPlayArgument(await getPublicKey()).then(
							async args => {
								// 创建播放实例成功后初始化
								oWebControl.JS_RequestInterface({
									funcName: "init",
									argument: JSON.stringify(args),
								}).then(function() {
									oWebControl.JS_RequestInterface({
										funcName: "startPreview",
										argument: JSON.stringify(
											HaikangConfig
											.startArgs),
									}).then(function() {})
								})
							})
					})
				}, function() { // 启动插件服务失败
				})
				// 创建 WebControl 实例成功
			},
			cbConnectError: function(e) {
				log('初始化海康插件错误', e);
				// 创建 WebControl 实例失败
			},
			cbConnectClose: function() {
				log('关闭海康插件成功');
				// bNormalClose = false 时表示异常断开
				// bNormalClose = true 时表示正常断开
			}
		})
	}
	
	ToolWindow.addWinResize(function() {
		if (oWebControl && dom) {
			oWebControl.JS_Resize(dom.offsetWidth, dom.offsetHeight);
		}
	})
	
	ToolWindow.addWinRefresh(() => {
		closeVideo()
	})
	
	async function getPublicKey() {
		// 获取公钥
		let oData = await oWebControl.JS_RequestInterface({
			funcName: "getRSAPubKey",
			argument: JSON.stringify({
				keyLength: 1024,
			}),
		})
		return oData.responseMsg.data
	}
</script>

<style>
</style>
